<?php

class RedisReader extends DataReader
{
    protected static $app = DEFAULT_REDIS_NAME;

    /**
     * @param string $app
     * @return PHPRedisStorage
     */
    public function redis($app = '')
    {
        if (! $app)
        {
		    $backtrace = debug_backtrace();
            $caller = $backtrace[1]['class'];
            
            if (class_exists($caller))
            {
                $tmp_vars = get_class_vars($caller);
                if (isset($tmp_vars['app']) && $tmp_vars['app'])
                {
                    $app = $tmp_vars['app'];
                }
            }
            if (! $app)
            {
                $app = self::$app;
            }
        }

        if ($app == DEFAULT_REDIS_NAME)
            p($backtrace);
        return RedisFactory::getInstance($app);
    }

    public static function getInstance($model)
    {
        static $instances = array();

        $model = strtolower($model);

        if (! isset($instances[$model]))
        {
            readredis($model);

            if (false !== ($pos = strpos($model, '/')))
            {
                $classname = substr($model, $pos + 1) . 'Redis';
            }
            else
            {
			    $classname = $model.'Redis';
            }

			$classname = ucwords($classname);
            $instances[$model] = new $classname();
        }
        return $instances[$model];
    }

    public function pager($curr_page = 1, $per_page = 10, $app = '')
    {
        include_once COMMON_LIB_PATH . 'Pager.class.php';
        if (! $app)
        {
		    $backtrace = debug_backtrace();
            $caller = $backtrace[1]['class'];

            if (class_exists($caller))
            {
                $tmp_vars = get_class_vars($caller);
                if (isset($tmp_vars['app']) && $tmp_vars['app'])
                {
                    $app = $tmp_vars['app'];
                }
            }
            if (! $app)
            {
                $app = self::$app;
            }
        }
        return new SnowPager($this->redis($app), $curr_page, $per_page, SnowPager::REDIS);
    }
}

?>